package ui.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import ui.controller.iController;

import domain.iObserver;
import domain.iSpelerTeken;
import domain.model.iObservable;

public class BoardViewChar extends JFrame implements iObserver {
	private static final long serialVersionUID = 1L;

	private JPanel board;
	private iController controller;
	private JButton[][] fields;
	private int locationSlot = 1;
	private String playerName = "nobody";
	private String enemyName = "nobody";

	public BoardViewChar(iController controller, String playername, int locationSlot)
			throws UIException {
		super();
		this.setLocationSlot(locationSlot);
		this.setPlayerName(playername);
		setController(controller);
		addComponents();
		finalizeLayout();
	}

	/*private void setIcon(){
		javax.swing.ImageIcon icon = new javax.swing.ImageIcon("./libraries/images/jasmic.png");
        javax.swing.JLabel label = new javax.swing.JLabel();
        label.setIcon(icon);
        board.add(label);
	}*/
	
	private void setController(iController controller) {
		this.controller = controller;
	}

	private void addComponents() throws UIException {
		board = new JPanel();
		
		board.setLayout(new GridLayout(controller.getNumberOfRows(), controller
				.getNumberOfColumns()));
		this.setContentPane(board);
		
		fields = new JButton[controller.getNumberOfRows()][controller
				.getNumberOfColumns()];

		for (int row = 0; row < fields.length; row++) {
			for (int column = 0; column < fields[0].length; column++) {
				JButton field = new JButton();
				field.setFont(new Font("Serif", Font.PLAIN, 72));
				field.addActionListener(new FieldActionListener());
				fields[row][column] = field;
				add(field);
			}
		}
	}

	private void finalizeLayout() {

		if (this.getEnemyName() == null) {
			this.setTitle(this.getPlayerName() + " - " + controller.getTitel());
		} else {
			this.setTitle(this.getPlayerName() + " ("
					+ controller.getSpelerTeken() + ") - "
					+ controller.getTitel() + " versus " + this.getEnemyName());
		}

		Dimension boardSize = new Dimension();
		boardSize.setSize(500, 500);
		setPreferredSize(boardSize);
		setLocation((this.getLocationSlot() * 500 + 25 * getLocationSlot()),
				100);

		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		/*ImageIcon imageIcon = new ImageIcon("./libraries/images/jasmic.png");
	    JButton button = new JButton(imageIcon);

	    board.add(button);
		*/
		
		pack();
	}

	// Inner class
	private class FieldActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			int rowClicked = -1;
			int columnClicked = -1;
			for (int row = 0; row < fields.length && rowClicked < 0; row++) {
				for (int column = 0; column < fields[0].length
						&& columnClicked < 0; column++) {
					if (fields[row][column] == e.getSource()) {
						rowClicked = row;
						columnClicked = column;
					}
				}
			}
			controller.play(rowClicked, columnClicked);
		}
	}

	public void redrawBoard() {
		iSpelerTeken[][] board = controller.getBoard().toArray();

		for (int row = 0; row < fields.length; row++) {
			for (int column = 0; column < fields[0].length; column++) {
				String valueAsString = null;
				try{
					valueAsString = Character.toString((Character)board[row][column].getTeken()).toUpperCase();
				}catch (NullPointerException e){
					valueAsString  = "";
				}
				
				fields[row][column].setText(valueAsString);

				if (!fields[row][column].getText().trim().isEmpty()) {
					fields[row][column].setEnabled(false);
				}
			}
		}
	}

	public void speelbaar() {
		setTitle("(" + this.getPlayerName() + "'s turn) "
				+ this.getPlayerName() + " (" + (Character)controller.getSpelerTeken().getTeken()
				+ ") - " + controller.getTitel() + " versus "
				+ this.getEnemyName());
	}

	public void onspeelbaar() {
		setTitle("(" + this.getEnemyName() + "'s turn) " + this.getPlayerName()
				+ " (" + (Character)controller.getSpelerTeken().getTeken() + ") - "
				+ controller.getTitel() + " versus " + this.getEnemyName());
	}
	
	public void redrawBoardGameOver(int[] getWinnendePosities) {
		fields[getWinnendePosities[0]][getWinnendePosities[1]].setBackground(Color.GREEN);
		fields[getWinnendePosities[2]][getWinnendePosities[3]].setBackground(Color.GREEN);
		fields[getWinnendePosities[4]][getWinnendePosities[5]].setBackground(Color.GREEN);
		
	}
	
	@Override
	public void update(iObservable subject) {
		redrawBoard();
		this.speelbaar();
	}

	private int getLocationSlot() {
		return locationSlot;
	}

	private void setLocationSlot(int locationSlot) {
		this.locationSlot = locationSlot;
	}

	private String getPlayerName() {
		return playerName;
	}

	private void setPlayerName(String playerName) {
		if (playerName != null && playerName != "") {
			this.playerName = playerName;
		}
	}

	private String getEnemyName() {
		return enemyName;
	}

	public void setEnemyName(String enemyName) {
		if (enemyName != null && enemyName != "") {
			this.enemyName = enemyName;
		}
	}

	

}
